<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>登录 - 应急管理</title>
  <link rel="icon" type="image/ico" href="pc/ico.png">
  <style>
    /* 全局样式 */
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: Arial, sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      background: linear-gradient(135deg, #ff4e50, #f9d423);
      color: #333;
    }

    .login-container {
      background-color: white;
      border-radius: 10px;
      padding: 2rem;
      max-width: 400px;
      width: 100%;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
      text-align: center;
      position: relative;
      overflow: hidden;
      transition: all 0.5s ease;
    }

    .login-container h1 {
      font-size: 1.8rem;
      color: #ff4e50;
      margin-bottom: 0.5rem;
      transition: opacity 0.5s ease;
    }

    .login-container p {
      font-size: 0.9rem;
      color: #666;
      margin-bottom: 2rem;
      transition: opacity 0.5s ease;
    }

    .login-type-toggle {
      display: flex;
      justify-content: center;
      margin-bottom: 1rem;
    }

    .login-type-toggle button {
      flex: 1;
      padding: 0.5rem 0;
      border: none;
      background-color: #f0f0f0;
      cursor: pointer;
      font-size: 1rem;
      transition: background-color 0.3s ease, color 0.3s ease;
    }

    .login-type-toggle button.active {
      background-color: #ff4e50;
      color: white;
    }

    .login-form {
      transition: opacity 0.5s ease, transform 0.5s ease;
    }

    .login-form.hidden {
      opacity: 0;
      transform: translateY(-20px);
      pointer-events: none;
    }

    .login-container input[type="text"],
    .login-container input[type="password"] {
      width: 100%;
      padding: 0.8rem;
      margin-bottom: 1rem;
      border: 1px solid #ddd;
      border-radius: 5px;
      font-size: 1rem;
      transition: border-color 0.3s ease;
    }

    .login-container input[type="text"]:focus,
    .login-container input[type="password"]:focus {
      border-color: #ff4e50;
      outline: none;
    }

    .login-container button[type="submit"] {
      width: 100%;
      padding: 0.8rem;
      background-color: #ff4e50;
      color: white;
      border: none;
      border-radius: 5px;
      font-size: 1rem;
      cursor: pointer;
      transition: background-color 0.3s ease;
    }

    .login-container button[type="submit"]:hover {
      background-color: #f43c42;
    }

    .login-container .footer {
      margin-top: 1.5rem;
      font-size: 0.9rem;
      color: #666;
    }

    .login-container .footer a {
      color: #ff4e50;
      text-decoration: none;
      transition: color 0.3s;
    }

    .login-container .footer a:hover {
      color: #f43c42;
    }

    /* 小屏幕适配 */
    @media (max-width: 480px) {
      .login-container {
        padding: 1.5rem;
      }

      .login-container h1 {
        font-size: 1.5rem;
      }

      .login-type-toggle button {
        font-size: 0.9rem;
      }

      .login-container input[type="text"],
      .login-container input[type="password"],
      .login-container button[type="submit"] {
        font-size: 0.9rem;
      }

      .login-container .footer {
        font-size: 0.8rem;
      }
    }
  </style>
</head>
<body>

<div class="login-container">
  <div class="login-type-toggle">
    <button id="userBtn" class="active" onclick="switchLoginType('user')">用户登录</button>
    <button id="teacherBtn" onclick="switchLoginType('teacher')">教师登录</button>
  </div>

  <h1 id="loginTitle">用户登录</h1>
  <p id="loginDescription">欢迎用户登录，请先登录您的账户</p>

  <form id="loginForm" class="login-form" onsubmit="handleLogin(event)">
    <input type="text" id="username" placeholder="用户名" required>
    <input type="password" id="password" placeholder="密码" required>
    <button type="submit">登录</button>
  </form>

  <div class="footer">
    <p>还没有账户？ <a href="#">立即注册</a></p>
  </div>
</div>

<script>
  let loginType = 'user'; // 默认是用户登录

  function switchLoginType(type) {
    if (loginType === type) return; // 如果已经是选中的类型，则不进行切换

    const userBtn = document.getElementById('userBtn');
    const teacherBtn = document.getElementById('teacherBtn');
    const loginTitle = document.getElementById('loginTitle');
    const loginDescription = document.getElementById('loginDescription');
    const loginForm = document.getElementById('loginForm');

    // 移除所有按钮的active类
    userBtn.classList.remove('active');
    teacherBtn.classList.remove('active');

    // 添加active类到当前选择的按钮
    if (type === 'user') {
      userBtn.classList.add('active');
      loginTitle.textContent = '用户登录';
      loginDescription.textContent = '欢迎用户登录，请先登录您的账户';
    } else {
      teacherBtn.classList.add('active');
      loginTitle.textContent = '教师登录';
      loginDescription.textContent = '欢迎教师登录，请先登录您的账户';
    }

    // 添加隐藏类以触发CSS动画
    loginForm.classList.add('hidden');

    // 等待动画完成后切换类型
    setTimeout(() => {
      loginType = type;
      loginForm.classList.remove('hidden');
    }, 500); // 与CSS的transition时间相匹配
  }

  async function handleLogin(event) {
    event.preventDefault(); // 阻止表单提交的默认行为

    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;

    // 使用统一的登录接口
    try {
      const response = await fetch('/api/v1/users/login', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({ username, password, type: loginType }) // 传递登录类型
      });

      const result = await response.json();

      if (result.code === 200) {
        // 登录成功，跳转到不同的页面
        if (loginType === 'user') {
          window.location.href = '/';
        } else {
          window.location.href = '/teacher_index.html';
        }
      } else {
        // 登录失败，显示错误信息
        alert(result.message || '登录失败，请重试');
      }
    } catch (error) {
      console.error("登录请求失败", error);
      alert('登录请求失败，请检查网络连接');
    }
  }
</script>

</body>
</html>
